package com.resifind.www.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.resifind.www.constant.ErrorCode;
import com.resifind.www.exception.BaseException;
import com.resifind.www.exception.Resp;
import com.resifind.www.po.User;
import com.resifind.www.po.UserFullInfo;
import com.resifind.www.service.ClientService;
import com.resifind.www.service.MerchantService;
import com.resifind.www.service.UserService;
import com.resifind.www.service.impl.ClientServiceImpl;
import com.resifind.www.service.impl.MerchantServiceImpl;
import com.resifind.www.service.impl.UserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/**
 * @author 36069
 */
@SuppressWarnings({"unused", "RedundantThrows"})
@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    private final UserService userService = new UserServiceImpl();
    private final ClientService clientService = new ClientServiceImpl();
    private final MerchantService merchantService = new MerchantServiceImpl();

    /**
     * 登录
     */
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        // 获取json字符串
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        String phoneNum = root.getString("phoneNum");
        String password = root.getString("password");
        try {
            Resp<User> loginUser = userService.login(phoneNum, password);
            if (loginUser.getData() != null) {
                // 将用户信息存入session
                req.getSession().setAttribute("user", loginUser.getData());
                // 响应
                resp.getWriter().write(JSON.toJSONString(loginUser));
            }
        } catch (BaseException e) {
            Resp<String> error = Resp.error(e.getCode(), e.getMsg());
            resp.getWriter().write(JSON.toJSONString(error));
        }
    }

    /**
     * 注册
     */
    public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        String phoneNum = root.getString("phoneNum");
        String password = root.getString("password");
        String userType = root.getString("userType");
        try {
            Resp<String> register = userService.register(phoneNum, password, userType);
            resp.getWriter().write(JSON.toJSONString(register));
        } catch (BaseException e) {
            Resp<String> error = Resp.error(e.getCode(), e.getMsg());
            resp.getWriter().write(JSON.toJSONString(error));
        }
    }

    /**
     * 忘记密码
     */
    public void forgetPassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        String phoneNum = root.getString("phoneNum");
        String password = root.getString("password");
        try {
            Resp<String> forgetPassword = userService.forgetPassword(phoneNum, password);
            resp.getWriter().write(JSON.toJSONString(forgetPassword));
        } catch (BaseException e) {
            Resp<String> error = Resp.error(e.getCode(), e.getMsg());
            resp.getWriter().write(JSON.toJSONString(error));
        }
    }

    /**
     * 检查登录状态
     */
    public void checkLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            Resp<String> error = Resp.error(ErrorCode.USER_NOT_LOGIN);
            resp.getWriter().write(JSON.toJSONString(error));
        } else {
            try {
                Resp<UserFullInfo<Object>> checkLogin = userService.checkLogin(user);
                if (checkLogin.getData() != null) {
                    // 将用户的附加信息存入session
                    req.getSession().setAttribute("addition", checkLogin.getData().getAdditionalInfo());
                }
                resp.getWriter().write(JSON.toJSONString(checkLogin));
            } catch (BaseException e) {
                Resp<String> error = Resp.error(e.getCode(), e.getMsg());
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }
    }

    /**
     * 退出登录
     */
    public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if (session != null) {
            session.invalidate();
        }
    }
}
